package com.app.aipass.activity;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.app.aipass.BR;
import com.app.aipass.R;
import com.app.aipass.app.AppViewModelFactory;
import com.app.aipass.databinding.ActivityQybBinding;
import com.app.aipass.entity.MyPacketEntity;
import com.app.aipass.entity.PacketListEntity;
import com.app.aipass.fragment.main.MyViewModel;
import com.app.aipass.utils.NumberUtils;
import com.app.aipass.view.BaseDialog;
import com.app.aipass.view.ConfirmDialog;
import com.app.aipass.view.InputPwdDialog;
import com.app.aipass.view.MyPacketAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.base.BaseActivity;

//权益宝
public class QybActivity extends BaseActivity<ActivityQybBinding, MyViewModel> {

    private ArrayList<RelativeLayout> list;

    private List<PacketListEntity.DataDTO> dataDTO;

    private int selectPos = 0;

    private InputPwdDialog inputPasswordDialog;

    private int page = 1;

    private MyPacketAdapter mAdapter;

    private int mMenuPos = 0;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_qyb;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public MyViewModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用NetWorkViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(MyViewModel.class);
    }

    @Override
    public void initData() {
        super.initData();
        list = new ArrayList<>();
        list.add(binding.rlOne);
        list.add(binding.rlTwo);
        list.add(binding.rlThree);
        list.add(binding.rlFour);
        list.add(binding.rlFive);
        binding.ivTitleBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        binding.twinklingRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                viewModel.getMyPacket();
                binding.twinklingRefreshLayout.finishRefresh();
            }
        });

        binding.twinklingRefreshLayout.setEnableLoadMore(false);

        binding.rlOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dataDTO != null) {
                    selectPos = 0;
                    selectPacket(binding.rlOne);
                    refreshProduct(dataDTO.get(selectPos));
                }
            }
        });

        binding.rlTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dataDTO != null) {
                    selectPos = 1;
                    selectPacket(binding.rlTwo);
                    refreshProduct(dataDTO.get(selectPos));
                }
            }
        });

        binding.rlThree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dataDTO != null) {
                    selectPos = 2;
                    selectPacket(binding.rlThree);
                    refreshProduct(dataDTO.get(selectPos));
                }
            }
        });

        binding.rlFour.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dataDTO != null) {
                    selectPos = 3;
                    selectPacket(binding.rlFour);
                    refreshProduct(dataDTO.get(selectPos));
                }
            }
        });

        binding.rlFive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dataDTO != null) {
                    selectPos = 4;
                    selectPacket(binding.rlFive);
                    refreshProduct(dataDTO.get(selectPos));
                }
            }
        });

        binding.tvBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                inputPasswordDialog = new InputPwdDialog(QybActivity.this, password -> {
                    viewModel.packetOrder(dataDTO.get(selectPos).getId(),password);
                });
                inputPasswordDialog.setOnDismissListener(dialog -> {

                });
                inputPasswordDialog.hideDialogOk();
                inputPasswordDialog.show();
            }
        });

        binding.tvMenuOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mMenuPos!=0){
                    mMenuPos = 0;
                    binding.tvMenuOne.setTextColor(Color.parseColor("#FFFFFF"));
                    binding.tvMenuTwo.setTextColor(Color.parseColor("#9599A6"));
                    binding.tvMenuThree.setTextColor(Color.parseColor("#9599A6"));
                    binding.tvMenuFour.setTextColor(Color.parseColor("#9599A6"));
                    viewModel.status = mMenuPos;
                    viewModel.getMyPacket();
                }
            }
        });

        binding.tvMenuTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mMenuPos!=1){
                    mMenuPos = 1;
                    binding.tvMenuTwo.setTextColor(Color.parseColor("#FFFFFF"));
                    binding.tvMenuOne.setTextColor(Color.parseColor("#9599A6"));
                    binding.tvMenuThree.setTextColor(Color.parseColor("#9599A6"));
                    binding.tvMenuFour.setTextColor(Color.parseColor("#9599A6"));
                    viewModel.status = mMenuPos;
                    viewModel.getMyPacket();
                }
            }
        });

        binding.tvMenuThree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mMenuPos!=2){
                    mMenuPos = 2;
                    binding.tvMenuThree.setTextColor(Color.parseColor("#FFFFFF"));
                    binding.tvMenuTwo.setTextColor(Color.parseColor("#9599A6"));
                    binding.tvMenuOne.setTextColor(Color.parseColor("#9599A6"));
                    binding.tvMenuFour.setTextColor(Color.parseColor("#9599A6"));
                    viewModel.status = mMenuPos;
                    viewModel.getMyPacket();
                }
            }
        });

        binding.tvMenuFour.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mMenuPos!=3){
                    mMenuPos = 3;
                    binding.tvMenuFour.setTextColor(Color.parseColor("#FFFFFF"));
                    binding.tvMenuTwo.setTextColor(Color.parseColor("#9599A6"));
                    binding.tvMenuThree.setTextColor(Color.parseColor("#9599A6"));
                    binding.tvMenuOne.setTextColor(Color.parseColor("#9599A6"));
                    viewModel.status = mMenuPos;
                    viewModel.getMyPacket();
                }
            }
        });

        viewModel.packetList();
        viewModel.getMyPacket();
    }

    private void selectPacket(RelativeLayout rlOne) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId() == rlOne.getId()) {
                list.get(i).setBackgroundResource(R.drawable.shape_radius_4_color_2e323f);
            } else {
                list.get(i).setBackground(null);
            }
        }
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.mPacketListEntity.observe(this, new Observer<PacketListEntity>() {
            @Override
            public void onChanged(PacketListEntity packetListEntity) {
                dataDTO = packetListEntity.getData();
                refreshProduct(dataDTO.get(selectPos));
            }
        });

        viewModel.mMyPacketEntity.observe(this, new Observer<MyPacketEntity>() {
            @Override
            public void onChanged(MyPacketEntity myPacketEntity) {
                mAdapter = new MyPacketAdapter(myPacketEntity.getData());
                binding.myrecyclerview.setAdapter(mAdapter);
                binding.myrecyclerview.setLayoutManager(new LinearLayoutManager(QybActivity.this));
                binding.myrecyclerview.setEmptyView(binding.emptyView);
                binding.myrecyclerview.setNestedScrollingEnabled(false);

                mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                    @Override
                    public void onItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                        if (view.getId() ==R.id.ivDelete){
                            List<MyPacketEntity.DataDTO> list = baseQuickAdapter.getData();
                            ConfirmDialog dialog = new ConfirmDialog(QybActivity.this);
                            dialog.setTitle("提示");
                            dialog.setMessage("确定撤销吗？");
                            dialog.setCancelable(false);
                            dialog.setYesOnclickListener("确定", new BaseDialog.OnYesClickListener() {
                                @Override
                                public void onYesClick() {
                                    viewModel.packetWithdraw(list.get(i).getId());
                                    dialog.dismiss();
                                }
                            });
                            dialog.setCancleClickListener("取消", new BaseDialog.OnCloseClickListener() {
                                @Override
                                public void onCloseClick() {
                                }
                            });
                            dialog.show();
                        }
                    }
                });
            }
        });
    }

    @SuppressLint("SetTextI18n")
    private void refreshProduct(PacketListEntity.DataDTO dataDTO) {
        binding.tvAllNum.setText(NumberUtils.keepPrecision(dataDTO.getSum_profit(), 2));
        binding.tvDayNum.setText(NumberUtils.keepPrecision(dataDTO.getDay_profit(), 2));
        binding.tvDays.setText(dataDTO.getLease_days() + "");
        binding.tvHaveNum.setText(dataDTO.getShop_num() + "/" + dataDTO.getMax_num());
        binding.tvPriceContent.setText(dataDTO.getPrice() + "AI");
    }
}
